package pipeline;

import java.util.ArrayList;
import java.util.Hashtable;

import setting.BioNLPSettings;
import util.io.FileUtil;

import def.BioNLPDocument;
import def.TSpan;

public class TriggerDetectionRefiner extends TriggerDetection {
	@Override public ArrayList<TSpan> detectTriggers(BioNLPDocument doc) {
		Hashtable<String, TSpan> pred = new Hashtable<String, TSpan>();
		String[] lines = FileUtil.getTextFromFile("temp_predictions/"
				+doc.fileId+BioNLPSettings.getValue("EventAnnotationSuffix")).split("\n");
		ArrayList<TSpan> predictions  = new ArrayList<TSpan>();
		for (int i = 0 ; i < lines.length;i++){
			if (lines[i].startsWith("T")){
				TSpan ts = new TSpan();
				ts.parse(lines[i]);
				pred.put(ts.tid, ts);
			}
			else if (lines[i].startsWith("E")){
				int start = lines[i].indexOf(":")+1;
				int end = lines[i].indexOf(" ");
				String key =lines[i].substring(start, end);
				if (pred.containsKey(key)){
					TSpan ts = pred.get(key);
					ts.tid = null;
					ts.textType = null;
					predictions.add(ts);
//					System.err.println("[PRED] "+pred.get(key));
					pred.remove(key);
				}
			}
		}
		return predictions;
	}
}
